package com.example.windowmanagersample.sensor

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.windowmanagersample.R

class SensorActivity : AppCompatActivity(), SensorEventListener {
    private var sensorManager: SensorManager? = null
    private var hingeAngleSensor: Sensor? = null

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_display_features)

        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        hingeAngleSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_HINGE_ANGLE)
    }

    override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {
        Log.d("WindowManager", "onAccuracyChanged $sensor, $accuracy")
    }

    override fun onSensorChanged(event: SensorEvent) {
        val angle = event.values[0]
        Log.d("WindowManager", "onSensorChanged $event, $angle")  // 获取角度值
    }

    override fun onResume() {
        super.onResume()
        hingeAngleSensor?.also { light ->
            sensorManager?.registerListener(this, light, SensorManager.SENSOR_DELAY_NORMAL)
        }
    }

    override fun onPause() {
        super.onPause()
        sensorManager?.unregisterListener(this)
    }
}
